/*
 * Created by JFormDesigner on Wed Jul 14 08:42:50 CST 2021
 */

package com.minelx.statementplus.ui.form.ui.memberbased.addingchildsql;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.minelx.statementplus.ui.form.service.tablemapping.ITableMapping;
import com.minelx.statementplus.ui.form.vo.ChildSqlBuilderPairValue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author aaa
 */
public class ChoosingChildSqlDialog extends JDialog {

	private final Consumer<ChildSqlBuilderPairValue> choosingSql;

	private final DefaultListModel<Map.Entry<String, Object>> directoryListModel;

	public ChoosingChildSqlDialog(Window owner,
	                              Consumer<ChildSqlBuilderPairValue> choosingSql,
	                              ITableMapping tableMapping) {
		super(owner, "", ModalityType.APPLICATION_MODAL);
		this.choosingSql = choosingSql;
		initComponents();

		directoryListModel = new DefaultListModel<>();
		tableMapping.customSql().entrySet().forEach(directoryListModel::addElement);
		sqlList.setModel(directoryListModel);

		DefaultListCellRenderer defaultListCellRenderer = new DefaultListCellRenderer();
		sqlList.setCellRenderer((list, value, index, isSelected, cellHasFocus) ->
				defaultListCellRenderer.getListCellRendererComponent(list, value.getKey(), index, isSelected, cellHasFocus));
	}

	private void okButtonActionPerformed(ActionEvent e) {
		Map.Entry<String, Object> selected = directoryListModel.getElementAt(sqlList.getSelectedIndex());
		choosingSql.accept(new ChildSqlBuilderPairValue(selected.getKey(), (String) selected.getValue()));
		dispose();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		scrollPane1 = new JScrollPane();
		sqlList = new JList<>();
		buttonBar = new JPanel();
		okButton = new JButton();
		label1 = new JLabel();

		//======== this ========
		setTitle("\u9009\u62e9\u9700\u8981\u7684\u67e5\u8be2");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		var contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));

				//======== scrollPane1 ========
				{

					//---- sqlList ----
					sqlList.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 20));
					scrollPane1.setViewportView(sqlList);
				}
				contentPanel.add(scrollPane1, new GridConstraints(0, 0, 1, 1,
					GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
					null, null, null));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
				buttonBar.setLayout(new GridBagLayout());
				((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 80};
				((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0};

				//---- okButton ----
				okButton.setText("\u9009\u62e9");
				okButton.setFont(new Font("Microsoft JhengHei Light", Font.BOLD, 19));
				okButton.addActionListener(e -> okButtonActionPerformed(e));
				buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);

			//---- label1 ----
			label1.setText("\u67e5\u8be2\u5217\u8868");
			label1.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 18));
			label1.setLabelFor(sqlList);
			dialogPane.add(label1, BorderLayout.NORTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		setSize(580, 435);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JScrollPane scrollPane1;
	private JList<Map.Entry<String, Object>> sqlList;
	private JPanel buttonBar;
	private JButton okButton;
	private JLabel label1;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
